<?php
$db_connect = mysqli_connect("localhost", "root", "", "battleofthemages");
session_start();
require_once("User.php");
require_once("Battle.php");
require_once("Card.php");

$battle = unserialize($_SESSION['battle']);
$battleId = $battle->getBattleId();

$roundCheckQuery = "SELECT user_one FROM round WHERE battle_id=$battleId";
$roundCheckResult = mysqli_query($db_connect, $roundCheckQuery);
$round = mysqli_num_rows($roundCheckResult);

if($_POST['action'] == "player"){
    $user = unserialize($_SESSION['user']);
    $playerProfilePic = $user->getProfilePic();
    echo "<img src=\"$playerProfilePic\" id=\"playerProfile\"/>";
    echo "<h2>" . $user->getUsername() . "</h2>";
    $rankQuery = "SELECT name FROM rank WHERE rank_id=" . $user->getRank();
    $rankQueryResult = mysqli_query($db_connect, $rankQuery);
    $row = mysqli_fetch_assoc($rankQueryResult);
    $rank = $row['name'];
    echo "<h3>" . $rank . "</h3>";
}

$battleQuery = "SELECT battle_id, user_one, user_two FROM battle WHERE battle_id=" . $battleId;
$result = mysqli_query($db_connect, $battleQuery);
$row = mysqli_fetch_assoc($result);
$userOneId = $row['user_one'];
$userTwoId = $row['user_two'];

$userOneQuery = "SELECT username, rank_id FROM user WHERE user_id=$userOneId";
$userOneResult = mysqli_query($db_connect, $userOneQuery);
$row = mysqli_fetch_assoc($userOneResult);
$userOneUsername = $row['username'];
$userOneRank = $row['rank_id'];

$userTwoQuery = "SELECT username, rank_id FROM user WHERE user_id=$userTwoId";
$userTwoResult = mysqli_query($db_connect, $userTwoQuery);
$row = mysqli_fetch_assoc($userTwoResult);
$userTwoUsername = $row['username'];
$userTwoRank = $row['rank_id'];

$user = unserialize($_SESSION['user']);
$username = $user->getUsername();

if($_POST['action'] == "opponent"){
    if($username == $userOneUsername){
        $playerProfilePic = $user->getProfilePic();
        echo "<img src=\"$playerProfilePic\" id=\"playerProfile\"/>";
        echo "<h2>" . $userTwoUsername . "</h2>";
        $rankQuery = "SELECT name FROM rank WHERE rank_id=" . $userTwoRank;
        $rankQueryResult = mysqli_query($db_connect, $rankQuery);
        $row = mysqli_fetch_assoc($rankQueryResult);
        $rank = $row['name'];
        echo "<h3>" . $rank . "</h3>";
    }else{
        $playerProfilePic = $user->getProfilePic();
        echo "<img src=\"$playerProfilePic\" id=\"playerProfile\"/>";
        echo "<h2>" . $userOneUsername . "</h2>";
        $rankQuery = "SELECT name FROM rank WHERE rank_id=" . $userOneRank;
        $rankQueryResult = mysqli_query($db_connect, $rankQuery);
        $row = mysqli_fetch_assoc($rankQueryResult);
        $rank = $row['name'];
        echo "<h3>" . $rank . "</h3>";
    }
}

if($_POST['action'] == "deal_cards_one"){
    $userOneDeck = array();

    $userOneDeckQuery = "SELECT ca.name as card_name, ca.attack as card_attack, ca.defence as card_defence, ca.link as card_link, ca.cost as card_cost, e.effect as card_effect
    FROM (((user u INNER JOIN deck d ON d.user_id=u.user_id)
     INNER JOIN deck_card dc ON dc.deck_id=d.deck_id)
      INNER JOIN card ca ON dc.card_id=ca.card_id)
      	LEFT JOIN effect e ON e.effect_id=ca.effect_id
       WHERE u.user_id=$userOneId";
    $userOneDeckResult = mysqli_query($db_connect, $userOneDeckQuery);
    if(mysqli_num_rows($userOneDeckResult) > 0){
        while($row = mysqli_fetch_assoc($userOneDeckResult)){
            $userOneDeck[] = new Card($row['card_name'], $row['card_attack'], $row['card_defence'], $row['card_link'], $row['card_cost'], $row['card_effect']);
        }
    }

    shuffle($userOneDeck);

    $userOneHand = array();
    for($i = 0; $i < 4; $i++){
        $temp = array_pop($userOneDeck);
        $userOneHand[$i] = $temp->getLink();
    }
    $hand = implode(";", $userOneHand);

    if($username == $userOneUsername){
        $updateRoundQuery = "UPDATE round SET user_one_hand=\"$hand\" WHERE battle_id=$battleId";
        mysqli_query($db_connect, $updateRoundQuery);
    }else if($username == $userTwoUsername){
        $updateRoundQuery = "UPDATE round SET user_two_hand=\"$hand\" WHERE battle_id=$battleId";
        mysqli_query($db_connect, $updateRoundQuery);
    }

    echo $hand;

    $_SESSION['user_one_hand'] = $hand;
    $_SESSION['user_one_deck'] = serialize($userOneDeck);

}else if($_POST['action'] == "deal_cards_two"){
    $userTwoDeck = array();

    $userTwoDeckQuery = "SELECT ca.name as card_name, ca.attack as card_attack, ca.defence as card_defence, ca.link as card_link, ca.cost as card_cost, e.effect as card_effect
    FROM (((user u INNER JOIN deck d ON d.user_id=u.user_id)
     INNER JOIN deck_card dc ON dc.deck_id=d.deck_id)
      INNER JOIN card ca ON dc.card_id=ca.card_id)
      	LEFT JOIN effect e ON e.effect_id=ca.effect_id
       WHERE u.user_id=$userTwoId";
    $userTwoDeckResult = mysqli_query($db_connect, $userTwoDeckQuery);
    if(mysqli_num_rows($userTwoDeckResult) > 0){
        while($row = mysqli_fetch_assoc($userTwoDeckResult)){
            $userTwoDeck[] = new Card($row['card_name'], $row['card_attack'], $row['card_defence'], $row['card_link'], $row['card_cost'], $row['card_effect']);
        }
    }

    shuffle($userTwoDeck);

    $userTwoHand = array();
    for($i = 0; $i < 4; $i++){
        $temp = array_pop($userTwoDeck);
        $userTwoHand[$i] = $temp->getLink();
    }
    $hand = implode(";", $userTwoHand);

    echo $hand;

    $_SESSION['user_two_hand'] = $hand;
    $_SESSION['user_two_deck'] = serialize($userTwoDeck);
}else if($_POST['action'] == "check_turn"){
    $checkQuery = "SELECT turn FROM round WHERE battle_id=$battleId";
    $checkResult = mysqli_query($db_connect, $checkQuery);
    $row = mysqli_fetch_assoc($checkResult);
    $id = $row['turn'];

    $userQuery = "SELECT username FROM user WHERE user_id=$id";
    $userResult = mysqli_query($db_connect, $userQuery);
    $row = mysqli_fetch_assoc($userResult);
    $user = $row['username'];
    echo "<span id=\"turn\">It`s $user`s turn</span>";
}else if($_POST['action'] == "end_turn"){
    $query = "SELECT user_one, user_two, user_one_hand, user_two_hand, turn FROM round WHERE battle_id=$battleId";
    $result = mysqli_query($db_connect, $query);
    $row = mysqli_fetch_assoc($result);
    if($row['turn'] == $row['user_one']){
        $turn = $row['user_two'];
        $notTurn = $row['user_one'];
    }else{
        $turn = $row['user_one'];
        $notTurn = $row['user_two'];
    }

    $query = "UPDATE round SET turn=$turn WHERE battle_id=$battleId";
    mysqli_query($db_connect, $query);

    $userOneDeck = unserialize($_SESSION['user_one_deck']);
    $userTwoDeck = unserialize($_SESSION['user_two_deck']);

    if($notTurn == $userOneId){
        $hand = $row['user_one_hand'];
        $countHand = explode(";", $hand);
        if(count($countHand) < 7){
            $temp = array_pop($userOneDeck);
            $hand .= ";";
            $hand .= $temp->getLink();
            $query = "UPDATE round SET user_one_hand=\"$hand\" WHERE battle_id=$battleId";
            mysqli_query($db_connect, $query);
        }
    }else if($notTurn == $userTwoId){
        $hand = $row['user_two_hand'];
        $countHand = explode(";", $hand);
        if(count($countHand) < 7){
            $temp = array_pop($userTwoDeck);
            $hand .= ";";
            $hand .= $temp->getLink();
            $query = "UPDATE round SET user_two_hand=\"$hand\" WHERE battle_id=$battleId";
            mysqli_query($db_connect, $query);
        }
    }

    $_SESSION['user_one_deck'] = serialize($userOneDeck);
    $_SESSION['user_two_deck'] = serialize($userTwoDeck);
}else if($_POST['action'] == "deal_hand_one"){
    $userOneDeck = unserialize($_SESSION['user_one_deck']);
    $userTwoDeck = unserialize($_SESSION['user_two_deck']);
    $userOneHand = $_SESSION['user_one_hand'];
    $userTwoHand = $_SESSION['user_two_hand'];

    $checkHandQuery = "SELECT user_one_hand, user_two_hand FROM round WHERE battle_id=$battleId";
    $checkHandResult = mysqli_query($db_connect, $checkHandQuery);
    $row = mysqli_fetch_assoc($checkHandResult);
    $oneHand = $row['user_one_hand'];
    $twoHand = $row['user_two_hand'];
    if($username == $userOneUsername){
        if($userOneHand != $oneHand){
            $_SESSION['user_one_hand'] = $oneHand;
            echo $oneHand;
        }else{
            echo "one_not_different";
        }
    }else if($username == $userTwoUsername){
        if($userTwoHand != $twoHand){
            $_SESSION['user_two_hand'] = $twoHand;
            echo $twoHand;
        }else{
            echo "two_not_different";
        }
    }else{
        echo "ERROR";
    }

    $_SESSION['user_one_deck'] = serialize($userOneDeck);
    $_SESSION['user_two_deck'] = serialize($userTwoDeck);
}else if($_POST['action'] == "deal_hand_two"){
    $userOneDeck = unserialize($_SESSION['user_one_deck']);
    $userTwoDeck = unserialize($_SESSION['user_two_deck']);
    $userOneHand = $_SESSION['user_one_hand'];
    $userTwoHand = $_SESSION['user_two_hand'];

    $checkHandQuery = "SELECT user_one_hand, user_two_hand FROM round WHERE battle_id=$battleId";
    $checkHandResult = mysqli_query($db_connect, $checkHandQuery);
    $row = mysqli_fetch_assoc($checkHandResult);
    $oneHand = $row['user_one_hand'];
    $twoHand = $row['user_two_hand'];
    if($username == $userOneUsername){
        if($userTwoHand != $twoHand){
            $_SESSION['user_two_hand'] = $twoHand;
            echo $twoHand;
        }else{
            echo "two_not_different";
        }
    }else if($username == $userTwoUsername){
        if($userOneHand != $oneHand){
            $_SESSION['user_one_hand'] = $oneHand;
            echo $oneHand;
        }else{
            echo "one_not_different";
        }
    }else{
        echo "ERROR";
    }

    $_SESSION['user_one_deck'] = serialize($userOneDeck);
    $_SESSION['user_two_deck'] = serialize($userTwoDeck);
}else if($_POST['action'] == "play_card"){
    if($username == $userOneUsername){
        $hand = $_SESSION['user_one_hand'];
        if(isset($_SESSION['user_one_field'])){
            $field = $_SESSION['user_one_field'];
        }else{
            $field = "";
        }

        if($field == ""){
            $temp = $_POST['cardUrl'];
            $field .= $temp;
            $query = "UPDATE round SET user_one_field=\"$field\" WHERE battle_id=$battleId";
            mysqli_query($db_connect, $query);

            $handArr = array();
            $handArr = explode(";", $hand);
            for($i = 0; $i < count($handArr); $i++){
                if($handArr[$i] == $temp){
                    unset($handArr[$i]);
                }
            }

            $hand = implode(";", $handArr);
            $handUpdateQuery = "UPDATE round SET user_one_hand=\"$hand\" WHERE battle_id=$battleId";
            mysqli_query($db_connect, $handUpdateQuery);
            $_SESSION['user_one_field'] = $field;
        }else{
            $countField = explode(";", $field);
            if(count($countField) < 4){
                $temp = $_POST['cardUrl'];
                $field .= ";";
                $field .= $temp;
                $query = "UPDATE round SET user_one_field=\"$field\" WHERE battle_id=$battleId";
                mysqli_query($db_connect, $query);

                $handArr = array();
                $handArr = explode(";", $hand);
                for($i = 0; $i < count($handArr); $i++){
                    if($handArr[$i] == $temp){
                        unset($handArr[$i]);
                    }
                }

                $hand = implode(";", $handArr);
                $handUpdateQuery = "UPDATE round SET user_one_hand=\"$hand\" WHERE battle_id=$battleId";
                mysqli_query($db_connect, $handUpdateQuery);
                $_SESSION['user_one_field'] = $field;
            }
        }
    }else if($username == $userTwoUsername){
        $hand = $_SESSION['user_two_hand'];
        if(isset($_SESSION['user_two_field'])){
            $field = $_SESSION['user_two_field'];
        }else{
            $field = "";
        }

        if($field == ""){
            $temp = $_POST['cardUrl'];
            $field .= $temp;
            $query = "UPDATE round SET user_two_field=\"$field\" WHERE battle_id=$battleId";
            mysqli_query($db_connect, $query);

            $handArr = array();
            $handArr = explode(";", $hand);
            for($i = 0; $i < count($handArr); $i++){
                if($handArr[$i] == $temp){
                    unset($handArr[$i]);
                }
            }


            $hand = implode(";", $handArr);
            $handUpdateQuery = "UPDATE round SET user_two_hand=\"$hand\" WHERE battle_id=$battleId";
            mysqli_query($db_connect, $handUpdateQuery);
            $_SESSION['user_two_field'] = $field;
        }else{
            $countField = explode(";", $field);
            if($countField != 1 && $countField != 0){
                $temp = $_POST['cardUrl'];
                $field .= ";";
                $field .= $temp;
                $query = "UPDATE round SET user_two_field=\"$field\" WHERE battle_id=$battleId";
                mysqli_query($db_connect, $query);

                $handArr = array();
                $handArr = explode(";", $hand);
                for($i = 0; $i < count($handArr); $i++){
                    if($handArr[$i] == $temp){
                        unset($handArr[$i]);
                    }
                }

                $hand = implode(";", $handArr);
                $handUpdateQuery = "UPDATE round SET user_two_hand=\"$hand\" WHERE battle_id=$battleId";
                mysqli_query($db_connect, $handUpdateQuery);
                $_SESSION['user_two_field'] = $field;
            }
        }
    }
}else if($_POST['action'] == "check_field_one"){
    if(isset($_SESSION['user_one_field'])){
        $userOneField = $_SESSION['user_one_field'];
    }else{
        $userOneField = "";
    }
    if(isset($_SESSION['user_two_field'])){
        $userTwoField = $_SESSION['user_two_field'];
    }else{
        $userTwoField = "";
    }
    $fieldQuery = "SELECT user_one_field, user_two_field FROM round WHERE battle_id=$battleId";
    $fieldQueryResult = mysqli_query($db_connect, $fieldQuery);
    $row = mysqli_fetch_assoc($fieldQueryResult);
    $userOneFieldDb = $row['user_one_field'];
    $userTwoFieldDb = $row['user_two_field'];

    $result = "";

    if($username == $userOneUsername){
        if($userOneField != $userOneFieldDb){
            $_SESSION['user_one_field'] = $userOneFieldDb;
            $result .= $userOneFieldDb . "|";
        }else{
            $result .= $userOneField . "|";
        }

        if($userTwoField != $userTwoFieldDb){
            $_SESSION['user_two_field'] = $userTwoFieldDb;
            $result .= $userTwoFieldDb;
        }else{
            $result .= $userTwoField;
        }
    }else if($username == $userTwoUsername){
        if($userTwoField != $userTwoFieldDb){
            $_SESSION['user_two_field'] = $userTwoFieldDb;
            $result .= $userTwoFieldDb . "|";
        }else{
            $result .= $userTwoField . "|";
        }

        if($userOneField != $userOneFieldDb){
            $_SESSION['user_one_field'] = $userOneFieldDb;
            $result .= $userOneFieldDb;
        }else{
            $result .= $userOneField;
        }
    }
    echo $result;
}

mysqli_close($db_connect);
?>